(function(pageName){
  sugarcane.module[pageName] = function(){
    $('.j-pagescreen').addClass('fn-hide');
    var urlParams = sugarcane.getUrlParams(),
      docType = 1,
      id = urlParams['id'];
      if (id == '0'){
        id = newsId;
      }
    if ($('#j-screen-'+pageName+id).length) {
      var pageScreen = $('#j-screen-'+pageName+id);
      pageScreen.removeClass('fn-hide');
      var _scrollTop = pageScreen.data('scrolltop') || 0;
      setTimeout(function(){
        $(window).scrollTop(_scrollTop);
      }, 10);
      return false;
    }
    var pageScreen = $('<div class="j-pagescreen" id="j-screen-'+pageName+id+'"></div>');
    $('body').append(pageScreen);

    datailShow();

    // 获取新闻
    var detailData = {};
    function datailShow(){
      if (id) {
        sugarcane.ajaxApi({
          "url": "/news/detail",
          "data": {"id": id},
          "callback": function(res){
            if (res['code'] == 0) {
              detailData = res['data'];
              sugarcane.twoShare({
                "id": res.data.id,
                "title": res.data.title,
                "desc": '分享来自经济日报客户端，更多精彩请下载http://d.jingjiribao.cn/index.html',
                "imgUrl": res.data.shareImage,
                "link": res.data.shareUrl,
                "docType": res.data.docType
              });
              if(res.data.docType){
                docType = res.data.docType;
              }
              // 标题
              pageScreen.append(newGrapes["newstitle"].init({"title":detailData['title']}));
              //document.title = detailData['title'];
              // 发布时间
              pageScreen.append(newGrapes["newstime"].init({"sub1":detailData['source'],"sub2":"发布时间："+detailData['publishTime'],"sub3":"","subright1":"","subright2":"","subright3":""}));

              // newsType值：1001（新闻）、1003（图集）、1004（外链）、1107（视频）、1108（入口）

              if (detailData['newsType'] == '1003') {
                var content = '';
                $.each(detailData['atlasList'], function(){
                  content += '<p><img src="'+this['imgUrl']+'" width="100%"></p><p>'+this['description']+'</p>';
                });
              }

              // 内容
              var newscontentObj = newGrapes["newscontent"].init({"content":content});
              pageScreen.append(newscontentObj);
              // shareBanner();
              sugarcane['shareBanner']({
                'id': id,
                'docType': docType,
                'containObj': pageScreen 
              });
            } else {
              showerrorView();
              peach.hiddenAppbar();
            }
          }
        });
      }
    }

    // function shareBanner(){
    //   if(sugarcane['app']['isShare'] && !pageScreen.find('.app-banner').length ){
    //     sugarcane.module.load('banner', function(listitemFn){
    //       var bannerObj = listitemFn({'id': id, 'docType': docType});
    //       pageScreen.append(bannerObj);
          
    //       var _height = $(bannerObj).css('height');
    //       pageScreen.css('margin-bottom',_height);
    //     });
    //   }
    // }
    
    function showerrorView(){
      var clientHeight = document.documentElement.clientHeight;
      pageScreen.css('position','relative').css('height',clientHeight+'px');
      var errorviewObj = $(`<div class="app-errorview">
          <img class="img" src="/static/img/isempty.png">
        <p class="error-tip">暂时没有数据</p>
      </div>`);
       pageScreen.append(errorviewObj);
    }
    
  };

  sugarcane.module['state'][pageName] = 'load';
  sugarcane.module.callback(pageName);
}('album'));
